﻿using System;
using System.Threading;
using System.Windows.Forms;
using ReportWizard.Forms;

namespace ReportWizard.Helpers
{
    class WaitProcess : IDisposable
    {
        private readonly Thread _process;
        public bool StopWaitProcess;
        private readonly int _steps;
        private ProgressForm _progressForm;

        
        public delegate void DefaultHandler();

        public int Step { get; set; }

        public WaitProcess(int steps)
        {
            _steps = steps;
            _process = new Thread(RunWaitProcess);
            _process.Start();
        }

        public void RunWaitProcess()
        {
            _progressForm = new ProgressForm(_steps);
            _progressForm.ShowDialog();
        }

        public void DoStep()
        {
            if (_progressForm != null)
            {
                if (_progressForm.InvokeRequired)
                {
                    _progressForm.Invoke(new MethodInvoker(DoStep));
                }
                else
                {
                    _progressForm.Increment();
                }
            }
        }

        public void Dispose()
        {
            _progressForm.Release();
        }
    }
}